<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const fn = (x, n) => {
        // let x = n / 2;
        // let y = n % 2;
        if(n<0) {
            x= 1/x
            n = -n;
        }
        let res = 1;
        while(n>0) {
            if(n%2===1) {
                // 计算
                res = res*x;
            }
            x *= x;
            n = Math.floor(n/2);
        }
        return res

        // let times = 0;
        // if (y === 1) {
        //   while () {
        //     times++;
        //     x = x/2;
        //   }
        // }
      };
      console.log(fn(2,-3));
      
    </script>
  </body>
</html>
